//
//  BirthdayViewController.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2018/1/16.
//  Copyright © 2018年 筱鹏. All rights reserved.
//

import UIKit

class BirthdayViewController: BaseViewController {

    var tableView: UITableView!
    var tableViewImpl: BirthdayTableViewImplement!
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.setNavigationBarHidden(true, animated: true)
        self.configSubview()
        self.registerNotification()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func configSubview() {
        tableView = UITableView(frame: self.view.bounds, style: .grouped)
        tableView.backgroundColor = Theme.color.background
        self.view.addSubview(tableView)
        
        tableView.translatesAutoresizingMaskIntoConstraints = false
        if #available(iOS 11.0, *) {
            NSLayoutConstraint(item: tableView!, attribute: .top, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
        } else {
            NSLayoutConstraint(item: tableView!, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 20).isActive = true
        }
        NSLayoutConstraint(item: tableView!, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: tableView!, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: tableView!, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 0).isActive = true
        
        tableViewImpl = BirthdayTableViewImplement(tableView)
        tableViewImpl.setupViewController(self)
        tableViewImpl.searchAll()
    }
    
    func registerNotification() {
        NotificationCenter.default.addObserver(self, selector: .researchDatasSel, name: .AddNewBirthday, object: nil)
    }
}

private extension Selector {
    static let researchDatasSel = #selector(BirthdayViewController.researchDatas)
}

extension BirthdayViewController {
    @objc func researchDatas() {
        self.tableViewImpl.searchAll()
    }
}

